package com.nd.qa.shareplate.util;

import java.util.zip.*;
import java.nio.charset.Charset;
import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.zip.ZipOutputStream;

import com.nd.qa.shareplate.exception.BizException;
public class ZipUtils {
	
	public static final int BUFFER_SIZE = 1024;
    public ZipUtils() {}
    /*
    * inputFileName 输入一个文件夹
    * zipFileName 目标文件夹路径
    */
    public void zip(String inputFileName,String outputFileName) throws Exception {
        String parent = new File(outputFileName).getParent();
        FileUtil.buildFile(parent,true);
        String zipFileName = outputFileName; //打包后文件名字
        System.out.println(zipFileName);
        zip(zipFileName, new File(inputFileName));
    }

    /**
     * 压缩文件
     * @param zipFileName 压缩文件名称
     * @param inputFile
     * @throws Exception
     */
    private void zip(String zipFileName, File inputFile) throws Exception {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        out.setEncoding("UTF-8");//47服务器上配置--GBK
        zip(out, inputFile, "");
        System.out.println("zip done");
        out.close();
    }

    /**
     * 压缩文件
     * @param out 输出流
     * @param f 输入文件
     * @param base 父级路径
     * @throws Exception
     */
    private void zip(ZipOutputStream out, File f, String base) throws Exception {
        if (f.isDirectory()) {
           File[] fl = f.listFiles();
           out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
           base = base.length() == 0 ? "" : base + "/";
           for (int i = 0; i < fl.length; i++) {
           zip(out, fl[i], base + fl[i].getName());
         }
        }else {
           out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
           FileInputStream in = new FileInputStream(f);
           int b;
           System.out.println("目录："+base);
           while ( (b = in.read()) != -1) {
            out.write(b);
         }
         in.close();
       }
    }

    /**
     * 解压 zip 文件
     * @param zipFile zip 压缩文件
     * @param destDir zip 压缩文件解压后保存的目录
     * @return 返回 zip 压缩文件里的文件名的 list
     * @throws Exception
     */
    public  List<String> unZip(File zipFile, String destDir) throws Exception {
        // 如果 destDir 为 null, 空字符串, 或者全是空格, 则解压到压缩文件所在目录
        if(StringUtils.isBlank(destDir)) {
            destDir = zipFile.getParent();
        }
        destDir = destDir.endsWith(File.separator) ? destDir : destDir + File.separator;
        ZipArchiveInputStream is = null;
        List<String> fileNames = new ArrayList<String>();
        try {
            is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile), BUFFER_SIZE));
            ZipArchiveEntry entry = null;
            while ((entry = is.getNextZipEntry()) != null) {
                fileNames.add(entry.getName());
                if (entry.isDirectory()) {
                    File directory = new File(destDir, entry.getName());
                    directory.mkdirs();
                } else {
                    OutputStream os = null;
                    try {
                        os = new BufferedOutputStream(new FileOutputStream(new File(destDir, entry.getName())), BUFFER_SIZE);
                        IOUtils.copy(is, os);
                    } finally {
                        IOUtils.closeQuietly(os);
                    }
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            IOUtils.closeQuietly(is);
        }
        return fileNames;
    }
    /**
     * 解压 zip 文件
     * @param zipfile zip 压缩文件的路径
     * @param destDir zip 压缩文件解压后保存的目录
     * @return 返回 zip 压缩文件里的文件名的 list
     * @throws Exception
     */
    public  List<String> unZip(String zipfile, String destDir) throws Exception {
        File zipFile = new File(zipfile);
        return unZip(zipFile, destDir);
    }
    public static boolean isPics(String filename)
    {
        boolean flag = false;

        if(filename.endsWith(".jpg") || filename.endsWith(".gif")  || filename.endsWith(".bmp") || filename.endsWith(".png"))
            flag = true;

        return flag;
    }

    public static void main(String [] temp){
        ZipUtils zip = new ZipUtils();
        try {
           zip.unZip("D:\\abc.zip", "D:\\abc");//你要压缩的文件夹
           //zip.unzip("D:\\CICODE\\CISCRIPTS\\script\\百度助手.zip",SysConstant.SOURCE_BASE_PATH+"百度助手",false);
        }catch (Exception ex) {
           ex.printStackTrace();
       }
    }
}